/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：SysOrganize仓储                                              
*│　作    者：sys                                            
*│　创建时间：2020-11-06 14:41:01  
*│　说    明：代码生成器生成   
*└──────────────────────────────────────────────────────────────┘
*/
using SXFramWork.Core;
using SXFramWork.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace SXFramWork.Repository
{
    public class SysOrganizeRepository:RepositoryAbstract<SysOrganizeEntity>,ISysOrganizeRepository
    {
        private IDbContext _dbContext;
        public SysOrganizeRepository(IDbContext dbContext):base(dbContext)
        {
            _dbContext = dbContext;
        }

        public async Task<SysOrganizeEntity> GetById(string id)
        {
            var sql = "SELECT a.Id,a.ParentId,a.Name,a.Sort,a.CreateTime,b.Name parentName FROM [SysOrganize] a left join SysOrganize b on a.ParentId=b.Id where a.id=@id";
            return await FromSql.GetDataAsync(sql, new { id });
        }

        public async Task<List<string>> GetChildren(string id)
        {
            var sql = @";with temp as(select * from SysOrganize where id=@id
                union all
                select a.* from SysOrganize a join temp b on a.ParentId=b.Id where b.Id is not null and b.Id <>''
                )
            select STUFF((select ','+id from temp for xml path('')),1,1,'')";
            var res = await FromSql.GetStrAsync(sql, new { id = id });
            return res.Split(',').ToList();
        }

        public async Task<List<SysOrganizeEntity>> GetParents(string orgId,IUnitOfWork unitOfWork)
        {
            var sql = @"with tb as
                (
                select * from SysOrganize where id=@orgId
                union all
                select a.* from SysOrganize a join tb b on a.id=b.ParentId where b.ParentId is not null and b.ParentId <>''
                ) 
                select id from tb";

            return await FromSql.GetDataListAsync(sql, new { orgId = orgId },unitOfWork);

        }

        public async Task<List<SysOrganizeEntity>> Query(OrgQueryModel model)
        {
            var sql = string.Empty;
            Dictionary<string, object> check = new Dictionary<string, object>();
            if (model.Id.IsNull())
            {
                if (!model.Name.IsNull())
                {
                    check.Add("a.Name", model.Name);
                }
                sql = "select a.id,a.Name,a.Sort,a.CreateTime,b.Name parentName from SysOrganize a left join SysOrganize b  on a.ParentId=b.Id where 1=1  ";
            }
            else
            {
                if (!model.Name.IsNull())
                {
                    check.Add("Name", model.Name);
                }
                sql = @";with temp as(select * from SysOrganize where id=@id
                union all
                select a.* from SysOrganize a join temp b on a.ParentId=b.Id where b.Id is not null and b.Id <>''
                )
                select*,(select name from SysOrganize where id=temp.ParentId)parentName from temp where 1=1 ";
            }
            var res = await FromSql.GetPageListAsync(sql, model,check,"", new { id = model.Id },"sort");
            return res;
        }
    }
}